import { createStore } from "vuex";

const store = createStore({
  state: {
    count: 102
  },
  mutations: {
    add(state) {
      state.count++;
    },
    red(state) {
      state.count--;
    }
  },
  actions: {
    // 同步增加
    addAction({ commit }) {
      commit('add');
    },
    // 异步增加：带参数
    addAsync({ commit }, time) {
      setTimeout(() => {
        commit('add');
      }, time);
    },
    // 同步减少
    redAction({ commit }) {
      commit('red');
    },
    // 异步减少：带参数
    redAsync({ commit }, time) {
      setTimeout(() => {
        commit('red');
      }, time);
    }
  }
});

export default store;
